#include<cstring>
#include<iostream>
using namespace std;

int main(void)
{
    char const* str = "abaababcbacbab";
    char const* strSub = "ababc";
    
    int len = strlen(strSub);
    int* next = new int[len];
    next[0] = -1;
    
    int i = 0;
    int j = -1;
    while( i < len - 1)
    {
        if (-1 == j || strSub[i] == strSub[j])
        {
            i++;
            j++;
            
            if (strSub[i] == strSub[j])
            {
                next[i] = next[j];
            }
            else
            {
                next[i] = j;
            }
        }
        else
        {
            j = next[j];
        }
    }
    
    int len1 = strlen(str);
    i = 0;
    j = 0;
    
    while (i < len1 && j < len)
    {
        if (-1 == j || str[i] == strSub[j])
        {
            i++;
            j++;
        }
        else
        {
            j = next[j];
        }
    }
    
    if (j == len)
    {
        cout<< "Find at index" << i - j + 1 << ": " << &str[i - j] <<endl;
    }
    else
    {
        cout<< "not find\n";
    }
    
    delete next;
    
    system("pause");
}
